package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Article;
import com.hl.sfzy.service.ArticleService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping("/add")
    public R add(@RequestBody Article article){
        int i = articleService.add(article);
        if(i>0){
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id){
        int i = articleService.remove(id);
        if(i>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/edit")
    public R edit(@RequestBody Article article){
        int i = articleService.edit(article);
        if(i>0){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        Article article = articleService.findById(id);
        return R.ok(article);
    }
    @GetMapping("/findAll")
    public R findAll(
            @RequestParam (defaultValue = "1") Integer pageNum,
            @RequestParam (defaultValue = "5") Integer pageSize,
            Article article
            ){
        PageInfo<Article> pageInfo = articleService.findAll(pageNum, pageSize, article);
        return R.ok(pageInfo);
    }
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Long> ids){
        int i = articleService.deleteBatch(ids);
        if(i>0){
            return R.ok("删除"+i+"条数据");
        }else {
            return R.fail("删除失败");
        }
    }
    @GetMapping("/findArticleConsultants")
    public R findArticleConsultants( Long id){
        List<Article> articleConsultants = articleService.findArticleConsultants(id);
        return R.ok(articleConsultants);
    }
}
